﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;

namespace aPCSuite
{
    public partial class frmNew : Form
    {
        private SMSFramework _SMSFramework = new SMSFramework();
        //private frmContact _frmContact = null;
        public event RefereshSMS _refresh_sms = null;
        public frmNew()
        {
            InitializeComponent();
            _SMSFramework.parent = this;
            this.Move += new EventHandler(MoveSubForm);
            ContextMenu cm = new ContextMenu();
            MenuItem mi = new MenuItem("Paste");
            mi.Click += new EventHandler(PasteAction);
            cm.MenuItems.Add(mi);
            richTextBox1.ContextMenu = cm;
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                richTextBox1.Text
                    += Clipboard.GetText(TextDataFormat.Text).ToString();
            }
        }  

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string snumber = richTextBox2.Text;
                if (snumber.Trim().Length > 0)
                {
                    string[] number_list = snumber.Split('\n');
                    snumber = "";
                    for (int i = 0; i < number_list.Length; ++i)
                    {
                        // process just get contact number
                        string _selected = number_list[i].Trim();
                        int index = _selected.IndexOf('<');
                        if (index > 0)
                        {
                            _selected = _selected.Substring(0, index);
                        }
                        snumber += _selected.Trim();
                        snumber += ",";
                    }
                    snumber = snumber.Substring(0, snumber.Length - 1);
                    string req = "<protocol encoding=\"UTF-8\" version=\"1.0\">";
                    req += "<app_id>1</app_id>";
                    req += "<session_id>" + Transaction._session_id + "</session_id>";
                    req += "<sms_send>";
                    req += "<dev_code>" + aPCSuite.Properties.Settings.Default.dev_code + "</dev_code>";
                    req += "<address>" + snumber + "</address>";
                    req += "<content>" + richTextBox1.Text + "</content>";
                    req += "</sms_send>";
                    req += "</protocol>";
                    _SMSFramework.srequest = req;
                    //_SMSFramework._callback += null;
                    _SMSFramework._callback += new callback(_callback);
                    _SMSFramework.Start();
                }
                else
                {
                    MessageBox.Show("Please enter phone number need to send to.");
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("btnOK_Click exception : {0}", ex.Message);
            }
        }

        private void btnShowContact_Click(object sender, EventArgs e)
        {
            try
            {
                if (frmMessaging._frmContact != null)
                    frmMessaging._frmContact.Close();
                frmMessaging._frmContact = new frmContact();
                frmMessaging._frmContact._contact_callback += new frmContact.ChooseContact(_contact_callback);
                frmMessaging._frmContact.Show(this);
                MoveSubForm(this, EventArgs.Empty);
                if (frmMessaging._contact_list.Count == 0)
                    frmMessaging._frmContact.GetContactList();
                else
                    frmMessaging._frmContact.SetDataSource();
                /*string req = "<protocol encoding=\"UTF-8\" version=\"1.0\">";
                req += "<app_id>1</app_id>";
                req += "<sms_contact>";
                req += "<dev_code>" + aPCSuite.Properties.Settings.Default.dev_code + "</dev_code>";
                req += "</sms_contact>";
                req += "</protocol>";
                _SMSFramework.srequest = req;
                _SMSFramework._callback += _callback;
                _SMSFramework.Start();/**/
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("btnShowContact_Click exception : {0}", ex.Message);
            }
        }

        private void _contact_callback(object sender, EventArgs e)
        {
            /*string contact_number = sender as string;
            if (richTextBox2.Text.Trim().Length > 0)
            {
                richTextBox2.Text += "\n";
            }
            richTextBox2.Text += contact_number;/**/
            richTextBox2.Text = "";
            List<string> number_list = sender as List<string>;
            string number_selected = "";
            if (number_list.Count > 0)
            {
                foreach (string item in number_list)
                {
                    number_selected += item;
                    number_selected += "\n";
                }
                number_selected = number_selected.Substring(0, number_selected.Length - 2);
                richTextBox2.Text = number_selected;
            }
        }

        protected void MoveSubForm(object sender, EventArgs e)
        {
            if (frmMessaging._frmContact != null)
            {
                frmMessaging._frmContact.Location = new Point(this.Location.X + this.Width + 10, this.Location.Y);
            }
        }

        private void _callback(object sender, EventArgs e)
        {
            try
            {
                protocol ret = sender as protocol;
                if (ret != null && ret.code == Transaction.OK)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.Close();
                        if (_refresh_sms != null)
                            _refresh_sms(sender, e);
                    }));
                }
                else
                {
                    //this.Invoke(new MethodInvoker(delegate { this.Close(); }));
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("frmContact _callback : {0}", ex.Message);
            }/**/
        }

    }
}
